package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.entity.Clazz;
import com.itheima.entity.ClazzQueryParam;
import com.itheima.entity.Result;
import com.itheima.entity.Student;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询
     * @param clazzParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzParam){
        log.info("clazzParam = {},{},{},{},{}", clazzParam.getName(), clazzParam.getBegin(), clazzParam.getEnd(), clazzParam.getPage(), clazzParam.getPageSize());

        //调用service查询数据
        return Result.success(clazzService.page(clazzParam));
    }

    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("clazz = {}", clazz);
        //调用service保存数据
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        return Result.success(clazzService.list());
    }

    /**
     * 查询班级数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("id = {}", id);
        //调用service查询数据
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("clazz = {}", clazz);
        //调用service更新数据
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("id = {}", id);
        //调用service删除数据
        clazzService.delete(id);
        return Result.success();
    }


}
